<template>
  <div>
    <div class="filter-container mt-15">
      <el-button type="primary" class="filter-item" icon="el-icon-plus" @click="handleAdd">添加配置</el-button>
      <el-upload
        ref="upload"
        class="filter-item"
        action="api/gm/load/config/upload"
        :headers="header"
        :limit="1"
        :on-success="successInfo"
        :file-list="fileList"
      >
        <el-button type="success" icon="el-icon-upload2">上传</el-button>
      </el-upload>
      <el-button :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
    </div>

    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" class="mt-15" height="685">
      <el-table-column label="ID" prop="id" align="center" width="100"></el-table-column>
      <el-table-column label="打点标识" prop="name" align="center" width="250"></el-table-column>
      <el-table-column label="排序" prop="sort" align="center" width="130"></el-table-column>
      <el-table-column width="130" align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope)">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog :visible.sync="dialogVisible" :title="textMap[dialogType]" :close-on-click-modal="false">
      <el-form ref="dataForm" :model="dataForm" :rules="rules" label-width="80px" label-position="left">
        <el-form-item label="ID" prop="id">
          <el-input v-model="dataForm.id" class="defaut-input" placeholder="ID" />
        </el-form-item>
        <el-form-item label="打点标识" prop="name">
          <el-input v-model="dataForm.name" class="defaut-input" placeholder="填写打点标识" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="dataForm.sort" class="defaut-input" placeholder="排序" />
        </el-form-item>
      </el-form>
      <div class="tr">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmSubmit()">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getLoadConfigList, handleLoadConfig } from '@/api/actorInfo'
import { deepClone } from '@/utils'

const defaultItem = {
  id: '',
  name: '',
  sort: '0'
}
export default {
  name: 'LoadConfig',
  data() {
    return {
      list: [],
      fileList: [],
      downloadLoading: false,
      listLoading: false,
      dataForm: Object.assign({}, defaultItem),
      header: {
        Authorization: this.$store.state.user.token
      },
      rules: {
        id: [{ required: true, message: '请填写ID', trigger: 'blur' }],
        name: [{ required: true, message: '请填写标识', trigger: 'blur' }]
      },
      dialogVisible: false,
      dialogType: 'new',
      textMap: {
        edit: '编辑',
        new: '新增'
      },
      listQuery: {
        page: 1,
        limit: 200
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getLoadConfigList(query)
      this.listLoading = false
      this.list = res.data
    },
    handleFilter() {
      this.getList()
    },
    handleAdd() {
      this.dataForm = Object.assign({}, defaultItem)
      this.dialogType = 'new'
      this.dialogVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.dataForm = deepClone(scope.row)
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    confirmSubmit() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          handleLoadConfig(this.dataForm).then(response => {
            if (response.status) {
              this.getList()
              this.dialogVisible = false
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            }
          })
        }
      })
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['ID', '打点标识', '排序']
        const filterVal = ['id', 'name', 'sort']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '打点配置'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    },
    successInfo(res, file, fileList) {
      this.getList()
      if (res.code === 40003) {
        this.$message.error(res.message)
      } else {
        this.$refs.upload.clearFiles()
        this.visible = false
        this.$message.success(res.message)
      }
    }
  }
}
</script>
